home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 April: Mac OS SDK / Dev.CD Apr 99 SDK1.toast / Development Kits / Zoomed Video Driver v1.0 SDK / Tools / PC Card DispNameReg / Src / EnumerateNameRegistry.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-06-02  |  9.3 KB  |  317 lines  |  [TEXT/CWIE]

  1. #include "SystemSoft.h"
  2. /*                                EnumerateNameRegistry.c                                */
  3. /*
  4.  * EnumerateNameRegistry.c
  5.  *
  6.  * Store the Name Registry into a two-level TwistDown list. The primary list has
  7.  * the path name and property name concatenated. Each element has a secondary list
  8.  * containing the element data. For example, suppose that the registry has the form:
  9.  *        Path A
  10.  *            First Property
  11.  *                First Property's Value
  12.  *            Second Property
  13.  *                Second Property's Value
  14.  *        Path B
  15.  *            First Property
  16.  *                First Property's Value
  17.  *            Second Property
  18.  *                Second Property's Value
  19.  * The resulting registry (REG) list will look like this:
  20.  *        Path A : First Property
  21.  *            First Property's Value
  22.  *        Path A : Second Property
  23.  *            Second Property's Value
  24.  *        Path B : First Property
  25.  *            First Property's Value
  26.  *        Path B : Second Property
  27.  *            Second Property's Value
  28.  * If this is then sorted on property (as opposed to the default of path), we get:
  29.  *        First Property
  30.  *            Path A
  31.  *                First Property's Value
  32.  *            Path B
  33.  *                First Property's Value
  34.  *        Second Property
  35.  *            Path A
  36.  *                First Property's Value
  37.  *            Path B
  38.  *                First Property's Value
  39.  */
  40. #include "DisplayNameRegistry.h"
  41.  
  42. void                        EnumeratePropertiesForThisName(
  43.         register BrowserPtr        browserPtr,
  44.         RegEntryID                *entryID,
  45.         UInt32                    regEntrySequence,
  46.         const RegCStrPathName    *pathName
  47.     );
  48. OSErr                        StoreThisDatum(
  49.         register BrowserPtr        browserPtr,
  50.         UInt32                    regEntrySequence,
  51.         const RegCStrPathName    *pathName,
  52.         const RegPropertyNameBuf foundProperty,
  53.         RegPropertyModifiers     propertyModifiers,
  54.         RegPropertyValueSize    propertySize,
  55.         const void                *propertyValue
  56.     );
  57. Boolean                        CheckForUserAbort(void);
  58.  
  59. /*
  60.  * This is adapted from the sample in the 8/3/94 PCI Drivers draft. Enumerate all
  61.  * paths and all propeties for each path. Store the entire collection in a sibling set
  62.  * stored in the Browser Record. The list will be then sorted and broken into primary
  63.  * and secondary sort keys according to the user's menu selection. On exit,
  64.  * BROWSER.registrySiblingSet.firstElement is the head of the list.
  65.  */
  66. void
  67. EnumerateNameRegistry(
  68.         BrowserPtr                browserPtr
  69.     )
  70. {
  71.         OSErr                    status;
  72.         RegEntryIter            cookie;
  73.         Boolean                    done;
  74.         RegEntryIterationOp        op;
  75.         RegEntryID                entry;
  76.         RegCStrPathName            *pathName;
  77.         RegPathNameSize            pathNameSize;
  78.         UInt32                    regEntrySequence;
  79.         
  80.         NewTwistDownSiblingSet(®);
  81.         op = kRegIterContinue;
  82.         regEntrySequence = 0;
  83.         status = RegistryEntryIterateCreate(&cookie);
  84.         CheckError(status, "\pRegistryEntryIterateCreate");
  85.         if (status == noErr) {
  86.             done = FALSE;
  87.             while (status == noErr && done == FALSE) {
  88.                 if (CheckForUserAbort())
  89.                     break;
  90.                 status = RegistryEntryIterate(&cookie, op, &entry, &done);
  91.                 CheckError(status, "\pRegistryEntryIterate");
  92.                 if (status == noErr && done == FALSE) {
  93.                     pathName = NULL;
  94.                     status = RegistryEntryToPathSize(&entry, &pathNameSize);
  95.                     CheckError(status, "\pRegistryEntryToPathSize");
  96.                     if (status == noErr) {
  97.                         pathName = (RegCStrPathName *) NewPtr(pathNameSize);
  98.                         if (pathName == NULL)
  99.                             status = MemError();
  100.                     }
  101.                     if (status == noErr) {
  102.                         status = RegistryCStrEntryToPath(&entry, pathName, pathNameSize);
  103.                         CheckError(status, "\pRegistryCStrEntryToPath");
  104.                     }
  105.                     if (status == noErr) {
  106.                         ++regEntrySequence;        /* Increment before using            */
  107.                         EnumeratePropertiesForThisName(
  108.                             browserPtr, &entry, regEntrySequence, pathName);
  109.                     }
  110.                     if (pathName != NULL)
  111.                         DisposePtr((Ptr) pathName);
  112.                     RegistryEntryIDDispose(&entry);
  113.                 }
  114.             }
  115.             RegistryEntryIterateDispose(&cookie);
  116.         }
  117. }
  118.  
  119. /*
  120.  * EnumeratePropertiesForThisName finds all properties for the current path. It creates
  121.  * a new registry list element (path : property) and a sub-list for the property values.
  122.  */
  123. void
  124. EnumeratePropertiesForThisName(
  125.         register BrowserPtr        browserPtr,
  126.         RegEntryID                *entryID,
  127.         UInt32                    regEntrySequence,
  128.         const RegCStrPathName    *pathName
  129.     )
  130. {
  131.         OSErr                    status;
  132.         RegPropertyIter            cookie;
  133.         Boolean                    done;
  134.         RegPropertyNameBuf        foundProperty;
  135.         RegPropertyValueSize    propertySize;
  136.         RegPropertyModifiers     propertyModifiers;
  137.         void                    *propertyValue;
  138.  
  139. #ifdef SYSF
  140.         if ((strncmp(WECARE, pathName, strlen(WECARE)) != 0) &&        // "Devices:device-tree:bandit:ti1130"
  141.             (strncmp(WECARED, pathName, strlen(WECARE)) != 0) )        // "Devices:device-tree:bandit:pci104c"
  142.             return;
  143. #endif
  144.  
  145.         status = RegistryPropertyIterateCreate(entryID, &cookie);
  146.         CheckError(status, "\pRegistryPropertyIterateCreate");
  147.         if (status == noErr) {
  148.             done = FALSE;
  149.             while (status == noErr && done == FALSE) {
  150.                 status = RegistryPropertyIterate(&cookie, foundProperty, &done);
  151.                 CheckError(status, "\pRegistryPropertyIterate");
  152.                 if (status == noErr && done == FALSE) {
  153.                     propertyValue = NULL;
  154.                     status = RegistryPropertyGetSize(
  155.                                 entryID, foundProperty, &propertySize);
  156.                     CheckError(status, "\pRegistryPropertyGetSize");
  157.                     if (status == noErr) {
  158.                         propertyValue = NewPtr(propertySize);
  159.                         if (propertyValue == NULL) {
  160.                             status = MemError();
  161.                             NonFatalError(status, "\pNo memory to store property");
  162.                         }
  163.                     }
  164.                     if (status == noErr) {
  165.                         status = RegistryPropertyGet(
  166.                                     entryID,
  167.                                     foundProperty,
  168.                                     propertyValue,
  169.                                     &propertySize
  170.                                 );
  171.                         CheckError(status, "\pRegistryPropertyGet");
  172.                     }
  173.                     if (status == noErr) {
  174.                         status = RegistryPropertyGetMod(
  175.                                     entryID,
  176.                                     foundProperty,
  177.                                     &propertyModifiers
  178.                                 );
  179.                         CheckError(status, "\pRegisteryPropertyGetMod");
  180.                     }
  181.                     if (status == noErr) {
  182.                         status = StoreThisDatum(
  183.                                     browserPtr,
  184.                                     regEntrySequence,
  185.                                     pathName,
  186.                                     foundProperty,
  187.                                     propertyModifiers,
  188.                                     propertySize,
  189.                                     propertyValue
  190.                                 );
  191.                         if (status != noErr)
  192.                             NonFatalError(status, "\pCould not store property");
  193.                     }
  194.                     if (propertyValue != NULL)
  195.                         DisposePtr((Ptr) propertyValue);
  196.                 }
  197.             }
  198.             RegistryPropertyIterateDispose(&cookie);
  199.         }
  200. }
  201.  
  202. /*
  203.  * StoreThisDatum stores a path : property : value in the list. Registry list elements
  204.  * are always locked in memory. The path + property is stored as two concatenated
  205.  * C-strings. This is the only routine that constructs a registry list element.
  206.  */
  207. OSErr
  208. StoreThisDatum(
  209.         register BrowserPtr        browserPtr,
  210.         UInt32                    regEntrySequence,
  211.         const RegCStrPathName    *pathName,
  212.         const RegPropertyNameBuf foundProperty,
  213.         RegPropertyModifiers     propertyModifiers,
  214.         RegPropertyValueSize    propertySize,
  215.         const void                *propertyValue
  216.     )
  217. {
  218.         OSErr                    status;
  219.         unsigned long            totalNameSize;
  220.         short                    pathNameSize;
  221.         short                    foundPropertySize;
  222. #define ELEM (**REG.thisElement)
  223.  
  224.         /*
  225.          * The name is stored as two concatenated C strings:
  226.          *    path EOS property EOS preceeded the binary sequence number.
  227.          */
  228.         pathNameSize = strlen(pathName);
  229.         foundPropertySize = strlen(foundProperty);
  230.         totalNameSize = sizeof regEntrySequence + pathNameSize + 1 + foundPropertySize + 1; 
  231.         status = MakeTwistDownSibling(®, 0, totalNameSize, NULL);
  232.         if (status == noErr) {
  233.             MoveHHi((Handle) REG.thisElement);
  234.             HLock((Handle) REG.thisElement);            /* Locked from now on        */
  235.             if ((propertyModifiers & kRegPropertyValueIsSavedToNVRAM) != 0)
  236.                 ELEM.flag |= kSavedInNVRAM;
  237.             if ((propertyModifiers & kRegPropertyValueIsSavedToDisk) != 0)
  238.                 ELEM.flag |= kSavedOnDisk;
  239.             BlockMoveData(
  240.                 ®EntrySequence,
  241.                 &ELEM.data[0],
  242.                 sizeof regEntrySequence
  243.             );
  244.             BlockMoveData(
  245.                 pathName,
  246.                 &ELEM.data[sizeof regEntrySequence],
  247.                 pathNameSize
  248.             );
  249.             /*
  250.              * Terminate the path name string.
  251.              */
  252.             ELEM.data[pathNameSize + sizeof regEntrySequence] = '\0';
  253.             BlockMoveData(
  254.                 foundProperty,
  255.                 &ELEM.data[sizeof regEntrySequence + pathNameSize + 1],
  256.                 foundPropertySize
  257.             );
  258.             ELEM.data[totalNameSize - 1] = '\0';        /* End of property string    */
  259.             if (propertySize > 0) {                        /* Property in sub-list        */
  260.                 /*
  261.                  * This will always create a sub-list. If the sub-list has a single
  262.                  * element, and it's short enough, the display module will scrunch
  263.                  * the secondary key and the data onto a single line of text.
  264.                  */
  265.                 ELEM.flag |= kHasTwistDown;
  266.                 ELEM.subElement = FormatThisProperty(
  267.                             foundProperty,
  268.                             propertySize,
  269.                             propertyValue
  270.                         );
  271.             }
  272.         }
  273.         return (status);
  274. #undef ELEM
  275. }
  276.  
  277. /*
  278.  * You must lock down the element before calling this routine. This is the only
  279.  * routine that unpacks a registry element. This function returns the registry
  280.  * entry sequence id that is needed to disambiguate between different entries
  281.  * with identical entry names.
  282.  */
  283. UInt32
  284. UnpackDataRecord(
  285.         const TwistDownPtr        thisElement,        /* Locked TwistDownHdl            */
  286.         char                    **pathNameCString,
  287.         char                    **foundPropertyCString
  288.     )
  289. {
  290. #define ELEM (*thisElement)
  291.         UInt32                    result;
  292.         
  293.         BlockMoveData(&ELEM.data[0], &result, sizeof result);
  294.         *pathNameCString = (char *) &ELEM.data[sizeof result];
  295.         *foundPropertyCString =
  296.             (char *) &ELEM.data[sizeof result + strlen(*pathNameCString) + 1];
  297.         return (result);
  298. #undef ELEM
  299. }
  300.  
  301. Boolean
  302. CheckForUserAbort(void)
  303. {
  304.         EventRecord                currentEvent;
  305.         Boolean                    result;
  306.  
  307.         EventAvail(everyEvent, ¤tEvent);
  308.         SpinCursor();
  309.         result = FALSE;
  310.         if ((currentEvent.what == keyDown || currentEvent.what == autoKey)
  311.          && (currentEvent.modifiers & cmdKey) != 0
  312.          && (currentEvent.message & keyCodeMask) == '.')
  313.              result = TRUE;
  314.         return (result);
  315. }
  316.  
  317.